package com.jlwl.houseservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jlwl.houseservice.entity.Feedback;
import com.jlwl.houseservice.mapper.FeedbackMapper;
import com.jlwl.houseservice.service.FeedbackService;
import com.jlwl.houseservice.vo.FeedbackCreateVO;
import com.jlwl.houseservice.vo.FeedbackQueryVO;
import com.jlwl.houseservice.vo.FeedbackReplyVO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

/**
 * 用户反馈Service实现类
 */
@Component
@org.springframework.stereotype.Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Override
    public Page<Feedback> pageList(FeedbackQueryVO queryVO) {
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        
        // 用户ID筛选
        if (queryVO.getUserId() != null) {
            queryWrapper.eq(Feedback::getUserId, queryVO.getUserId());
        }
        
        // 反馈类型筛选
        if (StringUtils.hasText(queryVO.getType())) {
            queryWrapper.eq(Feedback::getType, queryVO.getType());
        }
        
        // 状态筛选
        if (queryVO.getStatus() != null) {
            queryWrapper.eq(Feedback::getStatus, queryVO.getStatus());
        }
        
        // 日期范围筛选
        if (queryVO.getStartDate() != null) {
            queryWrapper.ge(Feedback::getCreatedAt, queryVO.getStartDate().atStartOfDay());
        }
        
        if (queryVO.getEndDate() != null) {
            queryWrapper.le(Feedback::getCreatedAt, queryVO.getEndDate().plusDays(1).atStartOfDay());
        }
        
        // 默认按创建时间倒序排序
        queryWrapper.orderByDesc(Feedback::getCreatedAt);
        
        // 分页查询
        return page(new Page<>(queryVO.getPage(), queryVO.getSize()), queryWrapper);
    }

    @Override
    @Transactional
    public boolean createFeedback(FeedbackCreateVO createVO, Integer userId) {
        Feedback feedback = new Feedback();
        feedback.setUserId(userId);
        feedback.setType(createVO.getType());
        feedback.setContent(createVO.getContent());
        
        // 处理图片
        if (createVO.getImages() != null && !createVO.getImages().isEmpty()) {
            feedback.setImages(String.join(",", createVO.getImages()));
        }
        
        feedback.setContactInfo(createVO.getContactInfo());
        feedback.setStatus(0); // 默认待处理
        feedback.setCreatedAt(LocalDateTime.now());
        feedback.setUpdatedAt(LocalDateTime.now());
        
        return save(feedback);
    }

    @Override
    @Transactional
    public boolean replyFeedback(Integer id, FeedbackReplyVO replyVO) {
        Feedback feedback = getById(id);
        if (feedback == null) {
            return false;
        }
        
        return update(new LambdaUpdateWrapper<Feedback>()
                .eq(Feedback::getId, id)
                .set(Feedback::getReply, replyVO.getReply())
                .set(Feedback::getReplyTime, LocalDateTime.now())
                .set(Feedback::getStatus, replyVO.getStatus())
                .set(Feedback::getUpdatedAt, LocalDateTime.now()));
    }

    @Override
    @Transactional
    public boolean updateFeedbackStatus(Integer id, Integer status) {
        Feedback feedback = getById(id);
        if (feedback == null) {
            return false;
        }
        
        return update(new LambdaUpdateWrapper<Feedback>()
                .eq(Feedback::getId, id)
                .set(Feedback::getStatus, status)
                .set(Feedback::getUpdatedAt, LocalDateTime.now()));
    }

    @Override
    public Page<Feedback> getUserFeedbacks(Integer userId, Integer page, Integer size) {
        return page(new Page<>(page, size), new LambdaQueryWrapper<Feedback>()
                .eq(Feedback::getUserId, userId)
                .orderByDesc(Feedback::getCreatedAt));
    }
} 